﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IEquipmentServices;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.EquipmentServices
{
    public class EquipmentledgerServices : IEquipmentledgerServices
    {

        public EquipmentledgerServices(IBaseRepository BaseConn)
        {
            this.BaseConn = BaseConn;
        }

        public IBaseRepository BaseConn { get; }
        public async Task<bool> AddAsync(dv_machinery model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }
        public async Task<bool> UpdateAsync(dv_machinery model)
        {
            var result = await BaseConn.UpdateAsync(model, "machinery_id");
            return result;
        }

        public async Task<bool> DellAllAsync(string[] Id)
        {
            var result = await BaseConn.DeleteAsync(@"delete dv_machinery where machinery_id in @Id", new { Id });
            return result;
        }

        public Task<bool> DellAsync(string Id)
        {
            var result = BaseConn.DeleteAsync(@"delete dv_machinery where machinery_id=@Id", new { Id = Id });
            return result;
        }

        public async Task<int> GetCountAsync(string mtName)
        {
            string sql = @"select count(1) from dv_machinery where  machinery_name like @mtName ";

            var count = await BaseConn.GetRecoreCountAsync(sql, new { mtName = "%" + mtName + "%" });

            return count;
        }

        public async Task<IEnumerable<dv_machinery>> GetJsonAsync(int PageIndex, int PageSize, string mtName, string code,string status)
        {
            string sql = @"select * from dv_machinery where 1=1";

            if (!string.IsNullOrEmpty(mtName))
            {
                sql += " and machinery_name like @mtName ";
            }
            if (!string.IsNullOrEmpty(code))
            {
                sql += " and machinery_code like @code ";
            }
            if (!string.IsNullOrEmpty(status))
            {
                sql += " and status like @status ";
            }
            sql += " order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only ";
            var list = await BaseConn.QueryAsync<dv_machinery>(sql, new { PageIndex, PageSize, mtName = "%" + mtName + "%", code = "%" + code + "%", status = "%" + status + "%" });

            return list;
        }


        public async Task<string> GetCodeasync()
        {
            var CodeNo = DateTime.Now.ToString("yyyy-MM-dd");
            string sql = @"select top(1)machinery_code from dv_machinery where machinery_code like @CodeNo  order by create_time desc ";
            var machinery_code = await BaseConn.QueryFirstOrDefaultAsync<string>(sql, new { CodeNo = "%" + CodeNo + "-%" });

            var Prefixs = "M";

            if (string.IsNullOrEmpty(machinery_code))
            {
                CodeNo = $"{Prefixs}-{CodeNo}-001";
            }
            else
            {
                var numberStr = machinery_code.Substring(machinery_code.LastIndexOf('-') + 1);

                int num = 10000;
                if (int.TryParse(numberStr, out num))
                {
                    num += 1;
                    if (num < 10000)
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-00{num}";
                    }
                    else if (num < 10100)
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-0{num}";
                    }
                    else
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-{num}";
                    }
                }
            }

            return CodeNo;
        }



    }
}
